package com.api.shopapi.mapper;

import com.api.shopapi.entity.ActivityPeople;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface ActivityPeopleMapper {

    @Insert("insert into activity_people (ActivityPeopleCode,ArticleCode,ElderlyCode,IdCard,Name,Phone,CreateTime) values " +
            "(#{activityPeople.ActivityPeopleCode},#{activityPeople.ArticleCode},#{activityPeople.ElderlyCode}," +
            "#{activityPeople.IdCard},#{activityPeople.Name},#{activityPeople.Phone},#{activityPeople.CreateTime})")
    int insert(@Param("activityPeople") ActivityPeople activityPeople);

    @Update("update activity_people set ArticleCode=#{activityPeople.ArticleCode},ElderlyCode=#{activityPeople.ElderlyCode}," +
            "IdCard=#{activityPeople.IdCard},Name=#{activityPeople.Name},Phone=#{activityPeople.Phone} " +
            "where ActivityPeopleCode=#{activityPeople.ActivityPeopleCode}")
    int update(@Param("activityPeople") ActivityPeople activityPeople);

    @Delete("delete from activity_people where ActivityPeopleCode=#{activityPeopleCode}")
    int delete(@Param("activityPeopleCode") String activityPeopleCode);

    @Select("select * from activity_people where ActivityPeopleCode=#{activityPeopleCode} limit 1")
    ActivityPeople getByActivityPeopleCode(@Param("activityPeopleCode") String activityPeopleCode);

    @Select("select * from activity_people where ArticleCode=#{articleCode} and Phone=#{phone} limit 1")
    ActivityPeople getByPhone(@Param("phone") String phone,@Param("articleCode") String articleCode);

    @Select("select * from activity_people where ArticleCode=#{articleCode} and IdCard=#{idCard} limit 1")
    ActivityPeople getByIdCard(@Param("idCard") String idCard,@Param("articleCode") String articleCode);

    @Select("<script>" +
            "select * from activity_people where ArticleCode=#{articleCode} "+
            "<if test ='name !=null and name!=\"\" '>" +
            "and Name like concat('%',#{name},'%') " +
            "</if>" +
            "order by CreateTime desc" +
            "</script>")
    List<ActivityPeople> getPage(@Param("articleCode") String articleCode,@Param("name")String name);

    @Select("select * from activity_people where ArticleCode=#{articleCode} and ElderlyCode=#{elderlyCode} limit 1")
    ActivityPeople getByElderlyCodeAndArticleCode(@Param("elderlyCode")String elderlyCode,@Param("articleCode") String articleCode);

}
